package br.unicamp.ft.sahp.model.dao.impl;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import br.unicamp.ft.sahp.model.dao.AbstractDAO;
import br.unicamp.ft.sahp.model.dao.RelatorioAvaliacaoDAO;
import br.unicamp.ft.sahp.model.entidades.RelatorioAvaliacao;
import br.unicamp.ft.sahp.model.exception.RelatorioAvaliacaoNaoEncontradoException;

@Repository
public class RelatorioAvaliacaoDAOImpl extends
		AbstractDAO<RelatorioAvaliacao, Long> implements RelatorioAvaliacaoDAO {

	@Autowired
	public RelatorioAvaliacaoDAOImpl(
			@Qualifier("entityManagerFactory") EntityManagerFactory factory) {
		super(factory, RelatorioAvaliacao.class);
	}

	public RelatorioAvaliacao findRelatorioByQuestionarioId(Long codigoQuestionario)
			throws RelatorioAvaliacaoNaoEncontradoException {
		
		String consulta = "SELECT relatorio FROM RelatorioAvaliacao relatorio "
				+ "WHERE relatorio.questionario.id = :idQuestionario";

		Query query = getEntityManager().createQuery(consulta);
		query.setParameter("idQuestionario", codigoQuestionario);

		RelatorioAvaliacao relatorio = (RelatorioAvaliacao) query
				.getSingleResult();

		if (relatorio == null) {
			System.out.println("nao encontrou relatorio!");
			throw new RelatorioAvaliacaoNaoEncontradoException();
		}
		
		return relatorio;
	}
}